﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Net.Http.Formatting;
using System.Threading;
using System.Threading.Tasks;
using System.Web;
using System.Web.Http;


namespace ULog.Common
{
    /// <summary>
    /// 
    /// </summary>
    public class ApiResult : IHttpActionResult
    {
     
     
        /// <summary>
        /// 是否正确为正确的结果
        /// </summary>
        public bool IsSuccess { get; set; }
        /// <summary>
        /// 是否包含业务逻辑出错，是：Message为业务出错信息，否：Message则为系统日常
        /// </summary>
        public bool HasBusinessError { get; set; }

        /// <summary>
        /// 错误信息，或者成功信息
        /// </summary>
        public string Message { get; set; }

        /// <summary>
        /// 成功时返回的数据
        /// </summary>
        public object Data { get; set; }
        /// <summary>
        /// 错误码，可供调用方自己决定怎么在前端展示出错信息
        /// </summary>
        public int ErrorCode { get; set; }
        public static ApiResult Error()
        {
            return new ApiResult()
            {
                IsSuccess = false
            };
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="message"></param>
        /// <returns></returns>
        public static ApiResult Error(string message)
        {
            return new ApiResult()
            {
                IsSuccess = false,
                Message = message
            };
        }
        /// <summary>
        /// 业务出错方法
        /// </summary>
        /// <param name="errorCode">错误代码</param>
        /// <returns></returns>
        public static ApiResult BusinessError(int errorCode)
        {
            return new ApiResult()
            {
                IsSuccess = false,
                HasBusinessError = true,
                ErrorCode = errorCode,
            };
        }
        /// <summary>
        /// 业务出错方法
        /// </summary>
        /// <param name="errorCode">错误代码</param>
        /// <param name="data">出错对象，方便调用方展示出错信息</param>
        /// <returns></returns>
        public static ApiResult BusinessError(int errorCode, object data)
        {
            return new ApiResult()
            {
                IsSuccess = false,
                HasBusinessError = true,
                ErrorCode = errorCode,
                Data = data
            };
        }

        /// <summary>
        /// 业务出错方法
        /// </summary>
        /// <param name="errorCode">错误代码</param>
        /// <param name="data">出错对象，方便调用方展示出错信息</param>
        /// <param name="msg">出错信息</param>
        /// <returns></returns>
        public static ApiResult BusinessError(int errorCode, object data,string msg)
        {
            return new ApiResult()
            {
                IsSuccess = false,
                HasBusinessError = true,
                ErrorCode = errorCode,
                Data = data,
                Message = msg
            };
        }


        public static ApiResult Success()
        {
            return new ApiResult()
            {
                IsSuccess = true
            };
        }
        public static ApiResult Success(string message)
        {
            return new ApiResult()
            {
                IsSuccess = true,
                Message = message
            };
        }
        public static ApiResult Success(object data)
        {
            return new ApiResult()
            {
                IsSuccess = true,
                Data = data
            };
        }
        public static ApiResult Success(object data, string message)
        {
            return new ApiResult()
            {
                IsSuccess = true,
                Data = data,
                Message = message
            };
        }

      

        public Task<HttpResponseMessage> ExecuteAsync(CancellationToken cancellationToken)
        {
            var response = new HttpResponseMessage()
            {
                Content = new ObjectContent(typeof(object), this, new JsonMediaTypeFormatter()),
            };
            return Task.FromResult(response);
        }

      


    }
}